package com.tjb.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.io.FileUtil;
import com.tjb.service.TsFileService;
import org.apache.commons.csv.CSVRecord;
import org.apache.tsfile.enums.TSDataType;
import org.apache.tsfile.file.metadata.enums.TSEncoding;
import org.apache.tsfile.fileSystem.FSFactoryProducer;
import org.apache.tsfile.read.common.Path;
import org.apache.tsfile.write.TsFileWriter;
import org.apache.tsfile.write.schema.MeasurementSchema;
import org.springframework.stereotype.Service;

import java.io.File;
import java.nio.file.Files;
import java.util.Collections;
import java.util.List;

@Service
public class TsFileServiceImpl implements TsFileService {
    @Override
    public void writeDataToTsFile(List<CSVRecord> batchData, File file) {
        System.out.println("开始进入文件写入流程");
        System.out.println("文件处理--------");
        //1、路径判断
        if (CollectionUtil.isEmpty(batchData) || FileUtil.isEmpty(file)){
            return;
        }
        //2、执行流程
        try {
            //2.1、存在文件就先删除文件
            String systemDirection = System.getProperty("user.dir");
            String fileName = file.getName();
            int dotIndex = fileName.lastIndexOf('.');
            if (dotIndex > 0 && dotIndex < fileName.length() - 1) {
                String nameWithoutExtension = fileName.substring(0, dotIndex);
                String extension = fileName.substring(dotIndex + 1);
            }
            File localFile = FSFactoryProducer.getFSFactory().getFile(systemDirection + fileName + ".tsfile");
            if (localFile.exists()) {
                Files.delete(localFile.toPath());
            }
            //开始写的流程
//            try (TsFileWriter tsFileWriter = new TsFileWriter(localFile)) {
//                System.out.println("创建序列-------");
//                //2.2、挂载的类型，根据自己的情况而定
//                MeasurementSchema measurementSchema = new MeasurementSchema(HeaderName.get(1), TSDataType.DOUBLE, TSEncoding.PLAIN);
//                //挂载到tsFileWriter
//                tsFileWriter.registerTimeseries(new Path("root.ln.tjb"), measurementSchema);
//                // 3、写入数据
//                write(tsFileWriter,HeaderName, objList);
//            }
        } catch (Exception e) {
            System.out.println("TsFileWriteWithTSRecord meet error: " + e);
        }
        //3、返回
    }
}
